package com.suse.lhw.resourceservere.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.foodserver.tools
 * @ClassName: FileUploader
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/8/5 16:30
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/8/5 16:30
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class FileUploader {
    public static String uploadAImg(MultipartFile multipartFile) {
        if (multipartFile.isEmpty())
            System.out.println("文件为空");
        String filename = multipartFile.getOriginalFilename();
        //得到文件名字
        String sufficeName = filename.substring(filename.indexOf("."));
        //得到后缀
//       String filepath ="D:\\AppFilesWithNoPriority\\毕业设计\\项目\\减肥管理系统\\health_manage_system\\resourceservere\\src\\main\\resources\\static\\image\\userImg\\";
        String filepath = "D:\\Users\\李鸿炜\\Desktop\\shit-mountain-of-health-manage-system\\resourceservere\\src\\main\\resources\\static\\image\\userImg";
        //文件上传后的路径
        filename = UUID.randomUUID() + sufficeName;
        //给一个新的名字
        File dest = new File(filepath + filename);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            multipartFile.transferTo(dest);
            System.out.println("图片以保存，名字是" + filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filename;
    }
}